const Boke = require("../models/boke")
exports.getboke = async function () {
    const result = await Boke.findAll()
    return result;
}
exports.addBoke = async function (obj) {
    const ins = await Boke.create(obj)
    return ins.toJSON()
}

exports.deleteBoke = async function (id) {
    return await Boke.destroy({
        where: {
            id
        }
    });
}

exports.getBokeById = async function (id) {
    const result = await Boke.findByPk(id);
    if (result) {
        return result.toJSON()
    }
    return null
}

exports.updateBoke = async function (id, Reading) {
    return await Boke.update({Reading}, {
        where: {
            id
        }
    });
}

exports.getIdfind = async function (id) {
    const result = await Boke.findOne({
        where:{
            id
        }
    });
    return result.toJSON()
}
exports.getfindall = async function (page = 1, limit = 2) {

    const result = await Boke.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
    })
    return JSON.parse(JSON.stringify(result))
}
const {
    Op
} = require("sequelize")
exports.getBookbyname = async function (page = 1, limit = 5, keywords = "") {
    const where = {
        [Op.or]: [{
            // Bokecontent: {
            //         [Op.like]: `%${keywords}%`
            //     }
            // },
            // {
            Boketit: {
                [Op.like]: `%${keywords}%`
            }
        }]
    };
    const result = await Boke.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where
    })
    return JSON.parse(JSON.stringify(result))
}

exports.getreading = async function (page = 1, limit = 5) {
    const result = await Boke.findAll({
        order: [
            ['Reading', 'DESC']
        ],
        offset: (page - 1) * limit,
        limit: +limit,
    })
    return result;
}

exports.getBookbyfenlei = async function (page = 1, limit = 5, keywords = "") {
    const where = {
        [Op.or]: [{
            // Bokecontent: {
            //         [Op.like]: `%${keywords}%`
            //     }
            // },
            // {
            fenlei: {
                [Op.like]: `%${keywords}%`
            }
        }]
    };
    const result = await Boke.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where
    })
    return JSON.parse(JSON.stringify(result))
}